package com.zwy.fornote.service.impl;

import com.zwy.fornote.dao.AnswerMapper;
import com.zwy.fornote.dao.UserMapper;
import com.zwy.fornote.entity.req.NewAnswerReq;
import com.zwy.fornote.entity.vo.AnswerVO;
import com.zwy.fornote.model.Answer;
import com.zwy.fornote.service.AnswerService;
import com.zwy.fornote.util.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 *
 * @author zhuyi 1205242
 * @version AnswerServiceImpl 2021/3/28 18:56
 */
@Service
public class AnswerServiceImpl implements AnswerService {
    @Autowired
    AnswerMapper answerMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public int answerQuestion(NewAnswerReq req) {
        Answer answer=new Answer();
        answer.setContent(req.getContent());
        answer.setDate(new Date());
        answer.setUserId(CurrentUser.getUser().getId());
        answer.setQuestionId(req.getQuestionId());
        return answerMapper.insert(answer);
    }

    @Override
    public List<AnswerVO> getAnswerByQuesId(int quesId) {
        List<Answer> answerByQuesId = answerMapper.getAnswerByQuesId(quesId);
        List<AnswerVO> voList=new ArrayList<>();
        for(Answer a:answerByQuesId)
        {
            AnswerVO vo=new AnswerVO();
            vo.setId(a.getId());
            vo.setContent(a.getContent());
            vo.setDate(a.getDate());
            vo.setUser(userMapper.selectByPrimaryKey(a.getUserId()));
            voList.add(vo);
        }
        return voList;
    }
}
